package cn.zysuyuan.item.service.impl;

import cn.zysuyuan.common.entity.PageResult;
import cn.zysuyuan.common.utils.UUIDUtil;
import cn.zysuyuan.item.entity.Edition;
import cn.zysuyuan.item.entity.FileEdition;
import cn.zysuyuan.item.mapper.*;
import cn.zysuyuan.item.service.EditionService;
import cn.zysuyuan.item.utils.HandleEditionFile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author flypig
 * @version 1.0
 * @date 2020-02-17 14:32
 */

@Service
public class EditionServiceImpl implements EditionService {

    @Autowired
    private EditionMapper editionMapper;

    @Autowired
    private  FileEditionMapper fileEditionMapper;

    @Autowired
    private CompanyMapper companyMapper;
    @Autowired
    private MedicinalMapper medicinalMapper;
    @Autowired
    private MedicinalProductBaseMapper medicinalProductBaseMapper;
    @Autowired
    private ProductbaseMedicinalGoneMapper productbaseMedicinalGoneMapper;
    @Autowired
    private ProductBaseMapper productBaseMapper;
    @Autowired
    private ProductBaseImgMapper productBaseImgMapper;

    @Autowired
    private CompanyEditionMapper companyEditionMapper;
    @Autowired
    private MedicinalEditionMapper medicinalEditionMapper;
    @Autowired
    private MedicinalProductBaseEditionMapper medicinalProductBaseEditionMapper;
    @Autowired
    private ProductbaseMedicinalGoneEditionMapper productbaseMedicinalGoneEditionMapper;
    @Autowired
    private ProductBaseEditionMapper productBaseEditionMapper;
    @Autowired
    private ProductBaseImgEditionMapper productBaseImgEditionMapper;


    @Override
    public PageResult<Edition> selectList(Map<String, Object> map) {
        List<Edition> list = editionMapper.selectList(map);
        return new PageResult<Edition>(list);
    }

    /**
     * 归档操作
     * 1.在公司   药材   药材基地关联表   药材-基地-原料去向关联表   基地    基地-图片关联表 的edition_time这一属性赋值上现在的datetime
     * 2.在上述属性的相关edition表中插入所有edition_time为这个datetime
     * 3.在edition表中插入一条新的归档记录
     */
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.READ_COMMITTED, rollbackFor=Exception.class)
    @Override
    public void add(Edition edition) {
        Date date =  new Date();// 归档时间
        FileEdition fileEdition = new FileEdition();

        //有关数据表update，将公司   药材   药材基地关联表   药材-基地-原料去向关联表   基地    基地-图片关联表  表中所有记录的edition_time属性值更新成现在的date
        companyMapper.updateEditionTime(date);
        //System.out.println(1/0);
        medicinalMapper.updateEditionTime(date);
        medicinalProductBaseMapper.updateEditionTime(date);
        productbaseMedicinalGoneMapper.updateEditionTime(date);
        productBaseMapper.updateEditionTime(date);
        productBaseImgMapper.updateEditionTime(date);

        //有关edition表插入记录
        companyEditionMapper.insert();
        medicinalEditionMapper.insert();
        medicinalProductBaseEditionMapper.insert();
        productbaseMedicinalGoneEditionMapper.insert();
        productBaseEditionMapper.insert();
        productBaseImgEditionMapper.insert();

        //edition信息初始化
        edition.setId(UUIDUtil.uuid());  //将这个归档记录赋值一条独一无二的id
        edition.seteTime(date);  //归档时间赋值

        //fileEdition信息初始化
        String name = edition.gettFileEditionId();
        fileEdition.setId(name);
        fileEdition.seteTime(date);

        fileEditionMapper.insert(fileEdition);
        editionMapper.insert(edition); //在归档表中插入这条记录

    }

    @Async
    @Override
    public void saveFile(String name) {
        try {
            HandleEditionFile handleEditionFile = new HandleEditionFile();
            handleEditionFile.CopeFile(name);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    @Override
    public int update(Edition edition) {
        return editionMapper.update(edition);
    }

    @Override
    public int delete(Edition edition) {

        //删除掉相关对象的edition表中的这个版本时间的记录
        companyEditionMapper.delete(edition.geteTime());
        medicinalEditionMapper.delete(edition.geteTime());
        medicinalProductBaseEditionMapper.delete(edition.geteTime());
        productbaseMedicinalGoneEditionMapper.delete(edition.geteTime());
        productBaseEditionMapper.delete(edition.geteTime());
        productBaseImgEditionMapper.delete(edition.geteTime());
        //productbaseEditionMapper.delete(edition.geteTime());//根据版本时间或者说是归档时间删除这个时间的基地信息

        HandleEditionFile.DeleteFile(edition.gettFileEditionId());
        fileEditionMapper.delete(edition.gettFileEditionId());

        //删掉edition表中这个时间版本的记录
        return editionMapper.delete(edition);
    }
}
